package com.banyuan.HomeWork.多线程;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/*
* 分别使用继承Thread、实现Runnable接口、实现Callable接口三种方式创建三个线程，
* 并发执行计算某个整数的阶乘,要求在main方法中打印出各个线程计算的结果
* */
public class QuestionThree {
    public static void main(String[] args) {
        method(10);
    }

    public static void method(int i){
        Thread th1 = new Thread(){
            int n = 1;
            @Override
            public void run() {
                for (int j = 1; j < i; j++) {
                    n *= j;
                    System.out.println(Thread.currentThread() + "_" +j);
                }
                System.out.println(n);
            }
        };
        Thread th2 = new Thread(new Runnable(){
            int n = 1;
            @Override
            public void run() {
                for (int j = 1; j < i; j++) {
                    n *= j;
                    System.out.println(Thread.currentThread() + "_" +j);
                }
                System.out.println(n);
            }
        });

        Thread th3 = new Thread(new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int n = 1;
                for (int j = 1; j < i; j++) {
                    n *= j;
                    System.out.println(Thread.currentThread() + "_" +j);
                }
                System.out.println(n);
                return n;
            }
        }));
        th1.start();
        th2.start();
        th3.start();
    }
}
